﻿using System;

namespace lambda实现泛型链表
{
    class Program
    {
        public class Node<T>
        {
            public Node<T> Next { get; set; }
            public T Data { get; set; }

            public Node(T t)
            {
                Next = null;
                Data = t;
            }
        }
        public class GenericList<T>
        {
            private Node<T> head;
            private Node<T> tail;

            public GenericList()
            {
                tail = head = null;
            }

            public Node<T> Head
            {
                get => head;
            }
            public void Add(T t)
            {
                Node<T> n = new Node<T>(t);
                if (tail == null)
                {
                    head = tail = n;
                }
                else
                {
                    tail.Next = n;
                    tail = n;
                }
            }
            public void ForEach(Action <T>action)
            {
                Node<T> p = this.head;
                while (p != null)
                {
                    action(p.Data);
                    p = p.Next;
                }
            }
            public void Max()
            {
                int max = Convert.ToInt32(this.head.Data);
                this.ForEach(m=> { if (Convert.ToInt32(m) > max) { max = Convert.ToInt32(m); } });
                Console.WriteLine("链表的最大值为：" + max);
            }
            public void Min()
            {
                int min = Convert.ToInt32(this.head.Data);
                this.ForEach(m => {
                    if (Convert.ToInt32(m) < min)
                    {
                        min = Convert.ToInt32(m);
                    }                   
                });
                Console.WriteLine("链表的最小值为：" + min);
            }
            public void print()
            {
                Console.WriteLine("链表遍历结果为");
                this.ForEach(m => Console.WriteLine(m));
            }
            public void Sum()
            {
                int sum = 0;
                this.ForEach(m =>  sum += Convert.ToInt32(m));
                Console.WriteLine("链表的总和为："+ sum);
            }

        }
        static void Main(string[] args)
        {
            GenericList<int> intlist = new GenericList<int>();
            while (true)
            {
                Console.WriteLine("请输入要选择的功能：" +
                "\n1.新建链表\n2.打印链表元素\n3.求最大值\n4.求最小值\n5.求和");
                string choice = Console.ReadLine();
                switch (choice)
                {
                    case "1":
                            Console.WriteLine("请输入要创建的整数型链表节点的个数：");
                            int n = Convert.ToInt32(Console.ReadLine());
                            for (int i = 0; i < n; i++)
                            {
                                Console.WriteLine("请输入第" + (i + 1) + "个整数型节点的值：");
                                int x = int.Parse(Console.ReadLine());
                                intlist.Add(x);
                            }                        
                        break;
                    case "2":
                        intlist.print();
                        break;
                    case "3":
                        intlist.Max();
                        break;
                    case "4":
                        intlist.Min();
                        break;
                    case "5":
                        intlist.Sum();
                        break;
                    default:
                        break;

                }
            }
            

            
            

          
        }
    }
}
